package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.IAppForm;
import com.shimao.celnet.payment.mapper.IAppInterfaceMapper;
import com.shimao.celnet.payment.mapper.IAppMapper;
import com.shimao.celnet.payment.model.IApp;
import com.shimao.celnet.payment.model.IAppInterface;
import com.shimao.celnet.payment.service.IAppService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;


/**
 * 接口访问应用系统 管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class IAppServiceImpl extends BaseServiceImpl<IApp> implements IAppService {
    protected Logger logger = Logger.getLogger(this.getClass());
    @Autowired(required = false)
    private IAppMapper localMapper;

    @Autowired(required = false)
    private IAppInterfaceMapper iAppInterfaceMapper;

    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public IAppForm getByPage(IAppForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(IApp.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getAppKey())) {
            //系统编号
            criteria.andLike("appKey", "%" + formBean.getAppKey() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getAppName())) {
            //系统名称
            criteria.andLike("appName", "%" + formBean.getAppName() + "%");
        }
        if (StringUtil.isNotEmpty(formBean.getEnable())) {
            //是否启用
            criteria.andEqualTo("enable", formBean.getEnable());
        }

        example.orderBy("appKey");
        //分页查询
        PageHelper.startPage(pageNum, length);
        List<IApp> tableList = super.getByExample(example);
        formBean.setPageInfo(tableList);
        return  formBean;
    }
/**  获取APP token**/
    @Override
    public String getAppTokenByAppkey(String appKey) {
        return localMapper.getAppTokenByAppkey(appKey);
    }

    /**
     *新增权限保存
     * @return
     */
    @Override
    public int save(IApp sBean){
        int retCnt=0;
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setGuid(getGuid());//guid
        String appUrl = sBean.getAppUrl();
        if(!FinalUtil.PERMISSION_MENU_BIAS.equals(StringUtils.right(appUrl,1))){
            sBean.setAppUrl(appUrl+FinalUtil.PERMISSION_MENU_BIAS);
        }
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改权限保存
     * @return
     */
    @Override
    public int update(IApp sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        String appUrl = sBean.getAppUrl();
        if(!FinalUtil.PERMISSION_MENU_BIAS.equals(StringUtils.right(appUrl,1))){
            sBean.setAppUrl(appUrl+FinalUtil.PERMISSION_MENU_BIAS);
        }
        int retCnt = super.update(sBean);

        return retCnt;
    }

    /**
     * 根据guid，删除信息
     * @param appGuid
     */
    @Override
    @CacheEvict(cacheNames="apps", allEntries=true)
    @Transactional(propagation= Propagation.REQUIRES_NEW,readOnly=false,rollbackFor={Exception.class})
    public int delete(String appGuid) {
        int retCnt = 0;
        logger.info("删除：根据系统guid，删除系统接口");
        Example example = new Example(IAppInterface.class);
        Example.Criteria criteria = example.createCriteria();
        //系统guid
        criteria.andEqualTo("appGuid", appGuid );
        iAppInterfaceMapper.deleteByExample(example);

        //删除应用系统
        logger.info("删除：根据guid，删除应用系统");
        retCnt = localMapper.deleteByPrimaryKey(appGuid);
        return retCnt;
    }

}
